// 精英敌机爆炸特效类
import { TimeManager } from './TimeManager.js';

export class EnemyEliteExplosion {
    constructor(x, y) {
        this.x = x;
        this.y = y;
        this.life = TimeManager.CONSTANTS.ELITE_EXPLOSION_LIFE || 2000; // 2秒爆炸效果
        this.maxLife = this.life;
        
        // 爆炸阶段
        this.phase = 0; // 0: 初始爆炸, 1: 主爆炸, 2: 余波
        this.phaseTimer = 0;
        
        // 主爆炸效果
        this.mainExplosionRadius = 0;
        this.maxMainRadius = 80;
        
        // 冲击波效果
        this.shockwaveRadius = 0;
        this.maxShockwaveRadius = 120;
        
        // 碎片系统
        this.debris = [];
        this.initDebris();
        
        // 火花系统
        this.sparks = [];
        this.initSparks();
        
        // 能量环
        this.energyRings = [];
        // this.initEnergyRings();
        
        // 烟雾粒子
        this.smokeParticles = [];
        this.initSmokeParticles();
    }
    
    initDebris() {
        // 创建机身碎片
        for (let i = 0; i < 12; i++) {
            const angle = (i / 12) * Math.PI * 2;
            const speed = Math.random() * 4 + 2;
            this.debris.push({
                x: this.x,
                y: this.y,
                vx: Math.cos(angle) * speed,
                vy: Math.sin(angle) * speed,
                rotation: Math.random() * Math.PI * 2,
                rotationSpeed: (Math.random() - 0.5) * 0.3,
                size: Math.random() * 8 + 4,
                color: ['#ff4444', '#ff6666', '#cc2222', '#990000'][Math.floor(Math.random() * 4)],
                life: this.maxLife * 0.8,
                maxLife: this.maxLife * 0.8
            });
        }
    }
    
    initSparks() {
        // 创建电火花效果
        for (let i = 0; i < 25; i++) {
            const angle = Math.random() * Math.PI * 2;
            const speed = Math.random() * 6 + 3;
            this.sparks.push({
                x: this.x,
                y: this.y,
                vx: Math.cos(angle) * speed,
                vy: Math.sin(angle) * speed,
                life: Math.random() * 800 + 400,
                maxLife: Math.random() * 800 + 400,
                brightness: Math.random() * 0.5 + 0.5,
                color: ['#ffff00', '#ffffff', '#ffaa00', '#ff6600'][Math.floor(Math.random() * 4)]
            });
        }
    }
    
    initEnergyRings() {
        // 创建能量冲击环
        for (let i = 0; i < 3; i++) {
            this.energyRings.push({
                radius: 0,
                maxRadius: 60 + i * 20,
                speed: 0.8 + i * 0.3,
                alpha: 0.8 - i * 0.2,
                delay: i * 100, // 延迟出现
                active: false
            });
        }
    }
    
    initSmokeParticles() {
        // 创建烟雾粒子
        for (let i = 0; i < 15; i++) {
            const angle = Math.random() * Math.PI * 2;
            const speed = Math.random() * 2 + 0.5;
            this.smokeParticles.push({
                x: this.x + (Math.random() - 0.5) * 40,
                y: this.y + (Math.random() - 0.5) * 40,
                vx: Math.cos(angle) * speed,
                vy: Math.sin(angle) * speed - 1, // 向上飘
                size: Math.random() * 20 + 10,
                maxSize: Math.random() * 40 + 30,
                alpha: 0.6,
                life: this.maxLife,
                maxLife: this.maxLife
            });
        }
    }
    
    update(deltaTime) {
        this.life = TimeManager.updateTimer(this.life, deltaTime);
        this.phaseTimer += deltaTime;
        
        // 更新爆炸阶段
        if (this.phaseTimer < 300) {
            this.phase = 0; // 初始冲击
        } else if (this.phaseTimer < 800) {
            this.phase = 1; // 主爆炸
        } else {
            this.phase = 2; // 余波
        }
        
        // 更新主爆炸半径
        if (this.phase <= 1) {
            this.mainExplosionRadius = Math.min(
                this.maxMainRadius, 
                (this.phaseTimer / 800) * this.maxMainRadius
            );
        }
        
        // 更新冲击波
        if (this.phase === 0) {
            this.shockwaveRadius = Math.min(
                this.maxShockwaveRadius,
                (this.phaseTimer / 300) * this.maxShockwaveRadius
            );
        }
        
        // 更新碎片
        for (let debris of this.debris) {
            debris.x += debris.vx * (deltaTime / 16.67);
            debris.y += debris.vy * (deltaTime / 16.67);
            debris.vy += 0.15 * (deltaTime / 16.67); // 重力
            debris.rotation += debris.rotationSpeed * (deltaTime / 16.67);
            debris.life = TimeManager.updateTimer(debris.life, deltaTime);
        }
        
        // 更新火花
        for (let spark of this.sparks) {
            spark.x += spark.vx * (deltaTime / 16.67);
            spark.y += spark.vy * (deltaTime / 16.67);
            spark.vx *= Math.pow(0.95, deltaTime / 16.67); // 阻力
            spark.vy *= Math.pow(0.95, deltaTime / 16.67);
            spark.life = TimeManager.updateTimer(spark.life, deltaTime);
        }
        
        // 更新能量环
        for (let ring of this.energyRings) {
            if (this.phaseTimer > ring.delay) {
                ring.active = true;
                ring.radius += ring.speed * (deltaTime / 16.67);
            }
        }
        
        // 更新烟雾粒子
        for (let smoke of this.smokeParticles) {
            smoke.x += smoke.vx * (deltaTime / 16.67);
            smoke.y += smoke.vy * (deltaTime / 16.67);
            smoke.size += (smoke.maxSize - smoke.size) * 0.02 * (deltaTime / 16.67);
            smoke.alpha *= Math.pow(0.98, deltaTime / 16.67);
            smoke.life = TimeManager.updateTimer(smoke.life, deltaTime);
        }
        
        return !TimeManager.isTimeUp(this.life);
    }
    
    render(ctx) {
        ctx.save();
        
        // 渲染冲击波
        if (this.phase === 0 && this.shockwaveRadius > 0) {
            this.renderShockwave(ctx);
        }
        
        // 渲染烟雾粒子（在背景）
        this.renderSmokeParticles(ctx);
        
        // 渲染主爆炸
        if (this.phase <= 1) {
            this.renderMainExplosion(ctx);
        }
        
        // 渲染能量环
        this.renderEnergyRings(ctx);
        
        // 渲染碎片
        this.renderDebris(ctx);
        
        // 渲染火花
        this.renderSparks(ctx);
        
        // 渲染闪光效果
        if (this.phase === 0) {
            this.renderFlash(ctx);
        }
        
        ctx.restore();
    }
    
    renderShockwave(ctx) {
        // 冲击波环
        for (let i = 0; i < 3; i++) {
            const radius = Math.max(0, this.shockwaveRadius - i * 15); // 确保半径非负
            if (radius > 0) {
                const alpha = (1 - this.shockwaveRadius / this.maxShockwaveRadius) * (1 - i * 0.3);
                
                ctx.strokeStyle = `rgba(255, 200, 100, ${alpha})`;
                ctx.lineWidth = 4 - i;
                ctx.beginPath();
                ctx.arc(this.x, this.y, radius, 0, Math.PI * 2);
                ctx.stroke();
            }
        }
    }
    
    renderMainExplosion(ctx) {
        // 主爆炸火球
        const progress = this.mainExplosionRadius / this.maxMainRadius;
        const alpha = 1 - progress * 0.7;
        const safeRadius = Math.max(0, this.mainExplosionRadius); // 确保半径非负
        
        if (safeRadius <= 0) return; // 如果半径不正数则不绘制
        
        // 外层火焰
        const outerGradient = ctx.createRadialGradient(
            this.x, this.y, 0,
            this.x, this.y, safeRadius
        );
        outerGradient.addColorStop(0, `rgba(255, 255, 255, ${alpha})`);
        outerGradient.addColorStop(0.3, `rgba(255, 200, 100, ${alpha * 0.8})`);
        outerGradient.addColorStop(0.6, `rgba(255, 100, 100, ${alpha * 0.6})`);
        outerGradient.addColorStop(1, 'rgba(200, 50, 50, 0)');
        
        ctx.fillStyle = outerGradient;
        ctx.beginPath();
        ctx.arc(this.x, this.y, safeRadius, 0, Math.PI * 2);
        ctx.fill();
        
        // 内层核心
        const coreRadius = Math.max(0, safeRadius * 0.4); // 确保核心半径非负
        if (coreRadius > 0) {
            const coreGradient = ctx.createRadialGradient(
                this.x, this.y, 0,
                this.x, this.y, coreRadius
            );
            coreGradient.addColorStop(0, `rgba(255, 255, 255, ${alpha})`);
            coreGradient.addColorStop(0.5, `rgba(255, 255, 200, ${alpha * 0.8})`);
            coreGradient.addColorStop(1, `rgba(255, 200, 100, ${alpha * 0.3})`);
            
            ctx.fillStyle = coreGradient;
            ctx.beginPath();
            ctx.arc(this.x, this.y, coreRadius, 0, Math.PI * 2);
            ctx.fill();
        }
    }
    
    renderEnergyRings(ctx) {
        for (let ring of this.energyRings) {
            if (ring.active && ring.radius < ring.maxRadius && ring.radius > 0) { // 添加半径正数检查
                const alpha = ring.alpha * (1 - ring.radius / ring.maxRadius);
                const safeRadius = Math.max(0, ring.radius); // 确保半径非负
                const safeInnerRadius = Math.max(0, ring.radius - 2); // 确保内圈半径非负
                
                ctx.strokeStyle = `rgba(100, 200, 255, ${alpha})`;
                ctx.lineWidth = 3;
                ctx.beginPath();
                ctx.arc(this.x, this.y, safeRadius, 0, Math.PI * 2);
                ctx.stroke();
                
                // 内部发光（只在半径足够大时绘制）
                if (safeInnerRadius > 0) {
                    ctx.strokeStyle = `rgba(200, 255, 255, ${alpha * 0.5})`;
                    ctx.lineWidth = 1;
                    ctx.beginPath();
                    ctx.arc(this.x, this.y, safeInnerRadius, 0, Math.PI * 2);
                    ctx.stroke();
                }
            }
        }
    }
    
    renderDebris(ctx) {
        for (let debris of this.debris) {
            if (!TimeManager.isTimeUp(debris.life)) {
                const alpha = debris.life / debris.maxLife;
                
                ctx.save();
                ctx.translate(debris.x, debris.y);
                ctx.rotate(debris.rotation);
                
                ctx.fillStyle = debris.color + Math.floor(alpha * 255).toString(16).padStart(2, '0');
                ctx.fillRect(-debris.size/2, -debris.size/2, debris.size, debris.size);
                
                // 发光边缘
                ctx.strokeStyle = `rgba(255, 200, 100, ${alpha * 0.5})`;
                ctx.lineWidth = 1;
                ctx.strokeRect(-debris.size/2, -debris.size/2, debris.size, debris.size);
                
                ctx.restore();
            }
        }
    }
    
    renderSparks(ctx) {
        for (let spark of this.sparks) {
            if (!TimeManager.isTimeUp(spark.life)) {
                const alpha = spark.life / spark.maxLife * spark.brightness;
                
                ctx.fillStyle = spark.color + Math.floor(alpha * 255).toString(16).padStart(2, '0');
                ctx.fillRect(spark.x - 1, spark.y - 1, 2, 2);
                
                // 拖尾效果
                ctx.strokeStyle = spark.color + Math.floor(alpha * 128).toString(16).padStart(2, '0');
                ctx.lineWidth = 1;
                ctx.beginPath();
                ctx.moveTo(spark.x, spark.y);
                ctx.lineTo(spark.x - spark.vx * 0.5, spark.y - spark.vy * 0.5);
                ctx.stroke();
            }
        }
    }
    
    renderSmokeParticles(ctx) {
        for (let smoke of this.smokeParticles) {
            if (!TimeManager.isTimeUp(smoke.life) && smoke.alpha > 0.01) {
                const safeSize = Math.max(0, smoke.size); // 确保尺寸非负
                
                if (safeSize > 0) {
                    const gradient = ctx.createRadialGradient(
                        smoke.x, smoke.y, 0,
                        smoke.x, smoke.y, safeSize
                    );
                    gradient.addColorStop(0, `rgba(60, 60, 60, ${smoke.alpha})`);
                    gradient.addColorStop(0.7, `rgba(40, 40, 40, ${smoke.alpha * 0.5})`);
                    gradient.addColorStop(1, 'rgba(20, 20, 20, 0)');
                    
                    ctx.fillStyle = gradient;
                    ctx.beginPath();
                    ctx.arc(smoke.x, smoke.y, safeSize, 0, Math.PI * 2);
                    ctx.fill();
                }
            }
        }
    }
    
    renderFlash(ctx) {
        // 强烈的白光闪烁
        if (this.phaseTimer < 150) {
            const alpha = 0.8 * (1 - this.phaseTimer / 150);
            const flashRadius = Math.max(0, this.maxMainRadius * 1.5); // 确保半径非负
            
            if (flashRadius > 0) {
                ctx.fillStyle = `rgba(255, 255, 255, ${alpha})`;
                ctx.beginPath();
                ctx.arc(this.x, this.y, flashRadius, 0, Math.PI * 2);
                ctx.fill();
            }
        }
    }
}